'use strict';
app.controller('goodsAnalysisCtrl',[ '$scope','$filter','utilService','$modal','goodsAnalysisService','toaster',function($scope,$filter,utilService,$modal,goodsAnalysisService,toaster) {
    $scope.start = new Date();
    $scope.end = new Date();
    $scope.start.setTime($scope.start.getTime()-24*60*60*30*1000);
    $scope.end.setTime($scope.end.getTime());
    $scope.analysis={
        analysisType:0,
        startTime:$scope.start,
        endTime:$scope.end,
        analysisStatus:0
    };

    //查询商品分析数据
    $scope.query=function(){
        $scope.analysis.startTime=$filter("date")($scope.analysis.startTime, "yyyy-MM-dd");
        $scope.analysis.endTime=$filter("date")($scope.analysis.endTime, "yyyy-MM-dd");
        goodsAnalysisService.findGoodsAnalysis($scope).then(function(result){
            switch (result.status){
                case 200:
                    $scope.analysisData=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }
    $scope.query();

    //查询每个商品的数据分析
    $scope.queryAll=function(){
        utilService.queryForPagingList($scope,{
            url:$scope.app.domain + 'goods/history',
            params:{
                startTime:$filter('date')($scope.analysis.startTime,'yyyy-MM-dd'),
                endTime:$filter('date')($scope.analysis.endTime,'yyyy-MM-dd')
            }
        }).then(function(result){
        })
    }
    $scope.queryAll();

    $scope.search=function(){
        $scope.query();
        $scope.queryAll();
    }

    //导出商品分析数据
    $scope.exportData=function(){
        openModal("确定要导出已查询出的记录吗？" +
            "(导出时间跟导出数据量的大小有关，请耐心等待)");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"温馨提示",
                controller:"exportGoodsAnalysisCtrl",
                message:msg,
                data: $scope.analysis
            });
        }
    }

    //导出每个商品的分析数据
    $scope.exportAllData=function(){
        openModal("确定要导出已查询出的记录吗？" +
            "(导出时间跟导出数据量的大小有关，请耐心等待)");
        function openModal(msg){
            utilService.modal($modal, $scope, {
                title:"温馨提示",
                controller:"exportAllGoodsAnalysisCtrl",
                message:msg,
                data: $scope.analysis
            });
        }
    }
}]);

//导出商品分析数据模态框Controller
app.controller('exportGoodsAnalysisCtrl', ['$scope', '$modalInstance','modalData','goodsAnalysisService','$filter',
    function($scope, $modalInstance,modalData,goodsAnalysisService,$filter) {
        $scope.modal = modalData;
        $scope.param = modalData.data;
        $scope.param.startTime=$filter('date')( $scope.param.startTime,'yyyy-MM-dd');
        $scope.param.endTime=$filter('date')( $scope.param.endTime,'yyyy-MM-dd');
        $scope.AllowToReport=true;

        $scope.ok=function(){
            if(!$scope.AllowToReport){
                return;
            }
            $scope.AllowToReport=false;
            goodsAnalysisService.exportData($scope);
            $modalInstance.dismiss('success');
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);
//导出每个商品的分析数据模态框Controller
app.controller('exportAllGoodsAnalysisCtrl', ['$scope', '$modalInstance','modalData','goodsAnalysisService','$filter',
    function($scope, $modalInstance,modalData,goodsAnalysisService,$filter) {
        $scope.modal = modalData;
        $scope.param = modalData.data;
        $scope.param.startTime=$filter('date')( $scope.param.startTime,'yyyy-MM-dd');
        $scope.param.endTime=$filter('date')( $scope.param.endTime,'yyyy-MM-dd');
        $scope.AllowToReport=true;

        $scope.ok=function(){
            if(!$scope.AllowToReport){
                return;
            }
            $scope.AllowToReport=false;
            goodsAnalysisService.exportAllData($scope);
            $modalInstance.dismiss('success');
        }
        //取消弹窗
        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }
]);

function getPreMonth(date,num) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    var day = arr[2]; //获取当前日期的日
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中月的天数
    var year2 = year;
    var month2=parseInt(month);

    for(var i=0;i<num;i++){
        month2 = parseInt(month2) - 1;
        if (month2 == 0) {
            year2 = parseInt(year2) - 1;
            month2 = 12;
        }
    }

    var day2 = day;
    var days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    if (day2 > days2) {
        day2 = days2;
    }
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    var t2 = year2 + '-' + month2 + '-' + day2;
    return t2;
}
